var app={
	fetch:function(url,method,data){
		return fetch(url, {
		  method: method,
		  headers: {
		    'Content-Type': 'application/json'
		  },
		  body: JSON.stringify(data)
		});
	},
	post:function(url, data, callback){
		var xhr = new XMLHttpRequest();
		  xhr.open('POST', url, true);
		  xhr.setRequestHeader('Authorization', 'Bearer token123'); // 添加自定义请求头
		
		  xhr.onreadystatechange = function() {
		     if(xhr.readyState === 4) {
		          if (xhr.status === 200) {
		            callback(true, JSON.parse(xhr.responseText)); // 成功的情况，将结果传递给回调函数
		          } else {
		            callback(false, null); // 失败的情况，将错误状态码传递给回调函数
		          }
		     }
		  };
		  xhr.send(JSON.stringify(data));
	},
	get:function(url,data, callback){
	  var xhr = new XMLHttpRequest();
	  var queryString = '';
	  if(data){
		  for (var key in data) {
		    if (data.hasOwnProperty(key) && data[key] !== null && data[key] !== '') {
		      if (queryString.length > 0) {
		        queryString += '&';
		      }
		      queryString += encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
		    }
		  }
	  }
	  // 将查询字符串拼接到 URL
	  if (queryString.length > 0) {
	    url += '?' + queryString;
	  }
	  xhr.open('GET', url, true);
	  xhr.setRequestHeader('Authorization', 'Bearer token123'); // 添加自定义请求头
	
	  xhr.onreadystatechange = function() {
		if(xhr.readyState === 4) {
		     if (xhr.status === 200) {
		       callback(true, JSON.parse(xhr.responseText)); // 成功的情况，将结果传递给回调函数
		     } else {
		       callback(false, null); // 失败的情况，将错误状态码传递给回调函数
		     }
		}
	  };
	  xhr.send();
	},
	addClickNodesRotateClassCallback(nodes,className,callback){
		nodes.forEach((e)=>{
			e.addEventListener('click',function(n){
				if(className){
					nodes.forEach((b)=>{ b.classList.remove(className); })
					e.classList.add(className);
				}
				callback(e);
			})
		})
	},
	addMouseOverNodesRotateClass(nodes,className){
		nodes.forEach((e)=>{
			e.addEventListener('mouseover',function(n){
				if(className){
					nodes.forEach((b)=>{ b.classList.remove(className); })
					e.classList.add(className);
				}
			})
		})
	},
	addClickNodesRotateClass(nodes,className){
		nodes.forEach((e)=>{
			e.addEventListener('click',function(n){
				if(className){
					nodes.forEach((b)=>{ b.classList.remove(className); })
					e.classList.add(className);
				}
			})
		})
	},
	//点击node 交替拥有和不拥有className
	addClickRotateClass(node,className){
		node.addEventListener('click',function(n){
			if(node.classList.contains(className)){
				node.classList.remove(className);
			}else{
				node.classList.add(className);
			}
		})
	},
	clickShowEventExt(mouseDom,showDom,left){
		mouseDom.addEventListener('click',function(e){
			 showDom.style.position ='absolute';
			 showDom.style.display = 'block';
			 //获取元素在屏幕里的可见地方
			 var rect = mouseDom.getBoundingClientRect();
			 showDom.style.left = (rect.left + left) + "px";
			 let top=rect.bottom + window.pageYOffset;
			 showDom.style.top = top + "px";
		})
		mouseDom.addEventListener('mouseout', function(e) {
			leave();
		});
		showDom.addEventListener('mouseout', function(e) {
			leave();
		});
		function leave(){
			var sourceDivRect = mouseDom.getBoundingClientRect(); // 获取该div元素相对于视口的位置信息
			var targetDivRect = showDom.getBoundingClientRect();
			var mouseX = event.clientX; // 获取鼠标当前位置的x坐标
			var mouseY = event.clientY; // 获取鼠标当前位置的y坐标
			if (mouseX >= sourceDivRect.left && mouseX <= sourceDivRect.right && mouseY >= sourceDivRect.top && mouseY <= sourceDivRect.bottom) {
				console.log('鼠标在div区域内');
			} else if( mouseX >= targetDivRect.left && mouseX <= targetDivRect.right && mouseY >= targetDivRect.top && mouseY <= targetDivRect.bottom){
				console.log('鼠标在div区域内');
			} else {
				console.log('两个区域都不在');
				showDom.style.display = 'none';
			}
		}
	},
	//鼠标悬浮在mouseDom时 showDom显示在mouseDom下面
	mouseShowEvent(mouseDom,showDom){
		mouseDom.addEventListener('mouseover',function(e){
			 showDom.style.position ='absolute';
			 showDom.style.display = 'block';
			 //获取元素在屏幕里的可见地方
			 var rect = mouseDom.getBoundingClientRect();
			 showDom.style.width = mouseDom.offsetWidth +"px";
			 showDom.style.left = rect.left + "px";
			 showDom.style.right= rect.right + "px";
			 showDom.style.top =  rect.top + mouseDom.offsetHeight + "px";
		})
		mouseDom.addEventListener('mouseout', function(e) {
			leave();
		});
		showDom.addEventListener('mouseout', function(e) {
			leave();
		});
		function leave(){
			var sourceDivRect = mouseDom.getBoundingClientRect(); // 获取该div元素相对于视口的位置信息
			var targetDivRect = showDom.getBoundingClientRect();
			var mouseX = event.clientX; // 获取鼠标当前位置的x坐标
			var mouseY = event.clientY; // 获取鼠标当前位置的y坐标
			if (mouseX >= sourceDivRect.left && mouseX <= sourceDivRect.right && mouseY >= sourceDivRect.top && mouseY <= sourceDivRect.bottom) {
				console.log('鼠标在div区域内');
			} else if( mouseX >= targetDivRect.left && mouseX <= targetDivRect.right && mouseY >= targetDivRect.top && mouseY <= targetDivRect.bottom){
				console.log('鼠标在div区域内');
			} else {
				console.log('两个区域都不在');
				showDom.style.display = 'none';
			}
		}
	},
	//鼠标在点击mouseDom时 showDom显示在mouseDom下面
	clickShowEvent:function(mouseDom,showDom){
		mouseDom.addEventListener('click',function(e){
			 showDom.style.position ='absolute';
			 showDom.style.display = 'block';
			 //获取元素在屏幕里的可见地方 
			 var rect = mouseDom.getBoundingClientRect();
			 let top=rect.bottom + window.pageYOffset;
			 showDom.style.left = rect.left + "px";
			 showDom.style.right= rect.right + "px";
			 showDom.style.top = top + "px";
		})
		mouseDom.addEventListener('mouseout', function(e) {
			leave();
		});
		showDom.addEventListener('mouseout', function(e) {
			leave();
		});
		function leave(){
			var sourceDivRect = mouseDom.getBoundingClientRect(); // 获取该div元素相对于视口的位置信息
			var targetDivRect = showDom.getBoundingClientRect();
			var mouseX = event.clientX; // 获取鼠标当前位置的x坐标
			var mouseY = event.clientY; // 获取鼠标当前位置的y坐标
			if (mouseX >= sourceDivRect.left && mouseX <= sourceDivRect.right && mouseY >= sourceDivRect.top && mouseY <= sourceDivRect.bottom) {
				console.log('鼠标在div区域内');
			} else if( mouseX >= targetDivRect.left && mouseX <= targetDivRect.right && mouseY >= targetDivRect.top && mouseY <= targetDivRect.bottom){
				console.log('鼠标在div区域内');
			} else {
				console.log('两个区域都不在');
				showDom.style.display = 'none';
			}
		}
	},
	//设置tab切换 nodes点击的节点列表  nodes_bound 绑定的待显示列表, className: 节点点击被选中时添加的className
	setTab:function(nodes,nodes_bound,className){
		nodes.forEach((node, index) => {
			 node.addEventListener('click', () => {
			   let num=index;
			   nodes.forEach(n => n.classList.remove(className));
			   node.classList.add(className);
			   setBoundItem(num);
			 });
		});
		
		function setBoundItem(num){
			nodes_bound.forEach((node, index) => {
				if(index==num){
					node.style.display='block';
				}else{
					node.style.display='none';
				}
			})
		}
	}
}